package Review;


public class Test15 {

	public static void main(String[] args) {
		ListNode p1 = new ListNode(1);
		ListNode p2 = new ListNode(2);
		ListNode p3 = new ListNode(3);
		ListNode p4 = new ListNode(4);
		p1.next=p2;
		p2.next=p3;
		p3.next=p4;
		p4.next=null;
		System.out.println(printLastK(p1,5).value);
	}
    public static ListNode printLastK(ListNode head,int k){
    	if(head==null||k<=0)
    		return null;
    	ListNode p1 = head;
    	for(int i=1;i<=k-1;i++){
    		if(p1.next!=null){
    			p1=p1.next;
    		}
    		else{
    			return null;
    		}
    	}
    	ListNode p2 = head;
    	while(p1.next!=null){
    		p1=p1.next;
    		p2=p2.next;
    	}
    	return p2;
    }
}
